package com.xdxc.factory;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;

@Component
public class RocketMQConsumerFactory {
    private DefaultLitePullConsumer consumer;

    public DefaultLitePullConsumer getOrCreateConsumer(String groupName) throws Exception {
        if (consumer == null || !consumer.getConsumerGroup().equals(groupName)) {
            if (consumer != null) consumer.shutdown();
            consumer = new DefaultLitePullConsumer(groupName);
            consumer.setNamesrvAddr("127.0.0.1:9876");
            consumer.setAutoCommit(false);
            consumer.start();
        }
        return consumer;
    }

    @PreDestroy
    public void destroy() {
        if (consumer != null) consumer.shutdown();
    }
}